using System;
using System.Collections;
using System.Data;
using SecuBat.Crawling.Forms;
using Css.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using SecuBat.Logging;

namespace SecuBat.Crawling
{
	/// <summary>
	/// Summary description for DatabaseManager.
	/// </summary>
	public class CrawlDbManager
	{
		public static int NewCrawlerRun(String name, DateTime beginTime, DateTime endTime)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand command = db.GetStoredProcCommand("newECrawlerRun");
			db.AddInParameter(command, "@i_Name", DbType.String, name);
			db.AddInParameter(command, "@i_BeginTime", DbType.DateTime, beginTime);
			if (endTime != DateTime.MinValue)
				db.AddInParameter(command, "@i_EndTime", DbType.DateTime, endTime);
			else
				db.AddInParameter(command, "@i_EndTime", DbType.DateTime, DBNull.Value);
			db.AddOutParameter(command, "@o_id", DbType.Int32, 4);

            if (db.ExecuteNonQuery(command) > 0)
			{
				return (int) db.GetParameterValue(command, "o_id");
			}
			return -1;
		}

		public static DataSet GetCrawlerRuns()
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand command = db.GetStoredProcCommand("lstECrawlerRun");

			DataSet ds = db.ExecuteDataSet(command);
			return ds;
		}

		public static int NewWebPage(int idRun, String url, String referer, int depth, DateTime crawlTime, int responseDuration)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand command = db.GetStoredProcCommand("newEWebPage");
			db.AddInParameter(command, "@i_IDCrawlerRun", DbType.Int32, idRun);
			db.AddInParameter(command, "@i_Url", DbType.String, url);
			if (referer != null)
				db.AddInParameter(command, "@i_Referer", DbType.String, referer);
			else
				db.AddInParameter(command, "@i_Referer", DbType.String, DBNull.Value);
			db.AddInParameter(command, "@i_Depth", DbType.Int32, depth);
			db.AddInParameter(command, "@i_CrawlTime", DbType.DateTime, crawlTime);
			db.AddInParameter(command, "@i_ResponseDuration", DbType.Int32, responseDuration);
			db.AddOutParameter(command, "@o_id", DbType.Int32, 4);

            if (db.ExecuteNonQuery(command) > 0)
			{
				return (int) db.GetParameterValue(command, "o_id");
			}
			return -1;
		}

		public static DataSet GetWebPages(int idRun)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand command = db.GetStoredProcCommand("lstEWebPage");
			db.AddInParameter(command, "@i_IDCrawlerRun", DbType.Int32, idRun);

			DataSet ds = db.ExecuteDataSet(command);
			return ds;
		}

		public static int NewWebForm(int idWebPage, String htmlForm, String actionAddress, String referrerAddress, String method, String encoding, DateTime crawlTime, int indexInWebPage)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand command = db.GetStoredProcCommand("newEWebForm");
			db.AddInParameter(command, "@i_IDWebPage", DbType.Int32, idWebPage);
			db.AddInParameter(command, "@i_HtmlForm", DbType.String, htmlForm);
			db.AddInParameter(command, "@i_ActionAddress", DbType.String, actionAddress);
			db.AddInParameter(command, "@i_ReferrerAddress", DbType.String, referrerAddress);
			db.AddInParameter(command, "@i_Method", DbType.String, method);
			db.AddInParameter(command, "@i_Encoding", DbType.String, encoding);
			db.AddInParameter(command, "@i_CrawlTime", DbType.DateTime, crawlTime);
			db.AddInParameter(command, "@i_IndexInWebPage", DbType.Int32, indexInWebPage);
			db.AddOutParameter(command, "@o_id", DbType.Int32, 4);

            if (db.ExecuteNonQuery(command) > 0)
			{
				return (int) db.GetParameterValue(command, "o_id");
			}
			return -1;
		}

		public static DataSet GetWebForms(int idWebPage)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand command = db.GetStoredProcCommand("lstEWebForm");
			db.AddInParameter(command, "@i_IDWebPage", DbType.Int32, idWebPage);

			DataSet ds = db.ExecuteDataSet(command);
			return ds;
		}

		public static WebForm GetWebForm(int idWebForm)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand command = db.GetStoredProcCommand("lstEWebForm");
			db.AddInParameter(command, "@i_ID", DbType.Int32, idWebForm);

			using (IDataReader dr = db.ExecuteReader(command))
			{
				if (dr.Read())
				{
					try
					{
						WebForm form = new WebForm(dr.GetInt32(0), dr.GetInt32(1), dr.GetString(2), new WebAddress(dr.GetString(3)), new WebAddress(dr.GetString(4)), (FormMethod) Enum.Parse(typeof(FormMethod),  dr.GetString(5)), dr.GetString(6), dr.GetInt32(8));
						return form;
					}
					catch (Exception ex)
					{
						LogManager.Write(TraceLevel.Error, ex, "Webform constructing");
					}
				}
			}
			return null;
		}

		public static ArrayList GetWebFormIds(int idCrawlerRun)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand command = db.GetStoredProcCommand("lstWebFormIdsByCrawlerRunId");
			db.AddInParameter(command, "@i_IDCrawlerRun", DbType.Int32, idCrawlerRun);

			ArrayList ids = new ArrayList();
			using (IDataReader dr = db.ExecuteReader(command))
			{
				while (dr.Read())
				{
					ids.Add(dr.GetInt32(0));
				}
			}
			return ids;
		}

		private static void BuildParameterStrings(FormParameterCollection parameters, ref String parameterNames, ref String parameterValues)
		{
			foreach (FormParameter param in parameters)
			{
				parameterNames += param.Name + ",";
				parameterValues += param.Value + ",";
			}
			if (parameterNames.Length > 0)
				parameterNames = parameterNames.Substring(0, parameterNames.Length - 1);
			if (parameterValues.Length > 0)
				parameterValues = parameterValues.Substring(0, parameterValues.Length - 1);
		}

	}
}
